﻿using System.Collections.Generic;

namespace FinalPointSet.Model.Asset
{
    public class TextAssetLoader
    {
        public static string GetString(string path)
        {
            return AssetUtil.LoadFileAsString(path) ?? string.Empty;
        }
        public static List<string> GetListString(string dir)
        {
            return AssetUtil.LoadDirectoryAsListString(dir);
        }

        /// <summary>
        /// 获取文件夹下的所有文件,加载文件为键值对集合
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <returns></returns>
        public static List<TextPair> GetTextListPair(string dir)
        {
            string[]? filePath = AssetUtil.GetFilesPath(dir);
            List<TextPair> textPairs = [];
            if (filePath == null)
            {
                return [];
            }
            foreach (string path in filePath)
            {
                string fileName = AssetUtil.GetFileName(path);
                string fileValue = AssetUtil.LoadAbsolutePathFileAsString(path);
                textPairs.Add(new TextPair(fileName, fileValue));
            }
            return textPairs;
        }

        /// <summary>
        /// 获取文件夹内所有文件的名称集合
        /// </summary>
        /// <param name="dir">文件夹路径</param>
        /// <returns></returns>
        public static List<TextPair> GetDirectoryFileFullNames(string dir)
        {
            string[]? filePath = AssetUtil.GetFilesPath(dir);
            List<TextPair> textPairs = [];
            if (filePath == null)
            {
                return [];
            }
            foreach (string path in filePath)
            {
                string fileName = AssetUtil.GetFileName(path);
                string fileFullName = AssetUtil.GetRelativePath(path);
                textPairs.Add(new TextPair(fileName, fileFullName));
            }
            return textPairs;
        }

        public static void SaveObject<T>(T obj, string path)
        {
            AssetUtil.SaveObjectAsJson(obj, path);
        }
        public static void SaveObjects<T>(List<T> objs, string path)
        {
            AssetUtil.SaveObjectsAsJson(objs, path);
        }
    }
}
